/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * formLaporanKeuangan.java
 *
 * 
 */
package modul.cetaklaporan.keuangan;

import java.io.File;
import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.swing.JOptionPane;
import model.util.conn.DataBaseConnection;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author Chipenkzz
 *
 */
public class formLaporanKeuangan extends javax.swing.JInternalFrame {

    /** Creates new form formLaporanKeuangan */
    public formLaporanKeuangan() {
        initComponents();
        this.setTitle("Laporan Keuangan");
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        lLaporan = new javax.swing.JLabel();
        pKeuangan = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jdcTanggalAwal = new com.toedter.calendar.JDateChooser();
        jdcTanggalAkhir = new com.toedter.calendar.JDateChooser();
        buttonTampil = new javax.swing.JButton();
        buttonTutup = new javax.swing.JButton();

        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/modul/util/image/report.png"))); // NOI18N

        jPanel2.setBackground(new java.awt.Color(204, 255, 204));

        lLaporan.setFont(new java.awt.Font("Tahoma", 1, 14));
        lLaporan.setText("FORM LAPORAN KEUANGAN");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(106, Short.MAX_VALUE)
                .addComponent(lLaporan, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(44, 44, 44))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lLaporan, javax.swing.GroupLayout.DEFAULT_SIZE, 24, Short.MAX_VALUE)
                .addContainerGap())
        );

        pKeuangan.setBorder(javax.swing.BorderFactory.createTitledBorder("Laporan Denda"));

        jLabel2.setText("Tanggal Awal");

        jLabel3.setText("Tanggal Akhir");

        jdcTanggalAwal.setDateFormatString("yyyy-MM-dd");

        jdcTanggalAkhir.setDateFormatString("yyyy-MM-dd");

        javax.swing.GroupLayout pKeuanganLayout = new javax.swing.GroupLayout(pKeuangan);
        pKeuangan.setLayout(pKeuanganLayout);
        pKeuanganLayout.setHorizontalGroup(
            pKeuanganLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pKeuanganLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pKeuanganLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(pKeuanganLayout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jdcTanggalAwal, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pKeuanganLayout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jdcTanggalAkhir, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap(38, Short.MAX_VALUE))
        );
        pKeuanganLayout.setVerticalGroup(
            pKeuanganLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pKeuanganLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pKeuanganLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(jdcTanggalAwal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pKeuanganLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(jdcTanggalAkhir, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(45, Short.MAX_VALUE))
        );

        buttonTampil.setText("Tampil");
        buttonTampil.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonTampilActionPerformed(evt);
            }
        });

        buttonTutup.setText("Tutup");
        buttonTutup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonTutupActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pKeuangan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(buttonTutup, javax.swing.GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE)
                    .addComponent(buttonTampil, javax.swing.GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pKeuangan, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addComponent(buttonTampil)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(buttonTutup)))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void buttonTutupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonTutupActionPerformed
    this.setVisible(false);// TODO add your handling code here:
}//GEN-LAST:event_buttonTutupActionPerformed

private void buttonTampilActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonTampilActionPerformed
    if (jdcTanggalAwal.getDate() != null || jdcTanggalAkhir.getDate() != null) {
        try {
            Properties systemProp = System.getProperties();
            String currentDir = systemProp.getProperty("user.dir");
            File dir = new File(currentDir);
            String reportName = "laporanKeuangan.jrxml";
            String gambar = "flower1.png";
            String reportDirName = "Report" + File.separatorChar + "Keuangan";
            File fileRpt;
            File fileGambar;
            String fullPathReport = "";
            String fullPathGambar = "";
            if (dir.isDirectory()) {
                String[] isiDir = dir.list();
                for (int i = 0; i < isiDir.length; i++) {
                    fileRpt = new File(currentDir + File.separatorChar + reportDirName + File.separatorChar + reportName);
                    fileGambar = new File(currentDir + File.separatorChar + reportDirName + File.separatorChar + gambar);
                    if (fileRpt.isFile()) {
                        fullPathReport = fileRpt.toString();
                    }
                    if (fileGambar.isFile()) {
                        fullPathGambar = fileGambar.toString();
                    }
                }
            }
            SimpleDateFormat spdt = new SimpleDateFormat(jdcTanggalAwal.getDateFormatString());
            String dtAwal = spdt.format(jdcTanggalAwal.getDate());
            String dtAhir = spdt.format(jdcTanggalAkhir.getDate());
            Connection conn = DataBaseConnection.getConnection();
            JasperReport jasperReport = JasperCompileManager.compileReport(fullPathReport);
            Map params = new HashMap();
            params.put("PRM_KD_TGL_AWAL", java.sql.Date.valueOf(dtAwal));
            params.put("PRM_KD_TGL_AKHIR", java.sql.Date.valueOf(dtAhir));
            params.put("PRM_KD_GAMBAR", fullPathGambar);
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, conn);
            JasperViewer.viewReport(jasperPrint, false);
        } catch (JRException ex) {
            JOptionPane.showMessageDialog(null, ex, "Error", JOptionPane.ERROR_MESSAGE);
        } catch (NoSuchMethodError ns) {
            JOptionPane.showMessageDialog(null, ns, "Error", JOptionPane.ERROR_MESSAGE);
        } catch (NullPointerException ns) {
            JOptionPane.showMessageDialog(null, ns, "Error", JOptionPane.ERROR_MESSAGE);
        }
    } else {
        JOptionPane.showMessageDialog(null, "Masukan tanggal dengan falid", "Peringatan", JOptionPane.WARNING_MESSAGE);
    }
}//GEN-LAST:event_buttonTampilActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonTampil;
    private javax.swing.JButton buttonTutup;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private com.toedter.calendar.JDateChooser jdcTanggalAkhir;
    private com.toedter.calendar.JDateChooser jdcTanggalAwal;
    private javax.swing.JLabel lLaporan;
    private javax.swing.JPanel pKeuangan;
    // End of variables declaration//GEN-END:variables
}
